package ssm.com.controler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import ssm.com.pojo.Door;
import ssm.com.service.DoorService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;


//控制层
@Controller
public class DoorController {

    //将业务层对象注入到控制层
    @Autowired
    private DoorService doorService;


    @RequestMapping("toAddDoorPage")
    public String toAddDoorPage(){
        return "addDoor";
    }

    @RequestMapping("addDoor")
    public String addDoor(Door door, Model model){
        //用业务层对象调用业务方法
        int row = doorService.addDoorInfo(door);
        if (row>0) {
            //绑定数据，跳转到列表页面
            //model.addAttribute("msg","添加成功");
            //重定向
            return "redirect:findAllDoorInfo";
        } else {
            //停留在添加页面，绑定数据回到页面
            model.addAttribute("msg","添加失败");
            return "addDoor";
        }
    }

    @RequestMapping("findAllDoorInfo")
    public String findAllDoorInfo(HttpServletRequest request){
        List<Door> list = doorService.findAllDoorInfo();
        request.setAttribute("doorList",list);
        return "door";
    }

    //根据id删除数据
    @RequestMapping("deleteDoorById")
    public String deleteDoorById(Integer id, HttpServletResponse response) throws IOException {
        int row = doorService.deleteDoorById(id);
        if (row<=0) {
            response.setContentType("text/html;charset=utf-8");
            PrintWriter writer = response.getWriter();
            writer.println("<script>alert('删除失败');location.href='findAllDoorInfo';</script>");
            writer.close();
        }
        //重定向
        return "redirect:findAllDoorInfo";
    }

    //根据id查询当前信息显示到修改页面
    @RequestMapping("findDoorByIdUpdate")
    public String findDoorByIdUpdate(Integer id,HttpSession session){
        //使用业务层对象调用业务方法
        Door door = doorService.findDoorByIdUpdate(id);
        session.setAttribute("door",door);
        return "updateDoor";
    }

    //修改后的更新操作
    //method = RequestMethod.POST表示只能接受post请求
    //method = RequestMethod.GET表示只能接受get请求
    //@RequestMapping(value = "toUpdate",method = RequestMethod.POST)
    @RequestMapping("toUpdateDoor")
    public String toUpdateDoor(Door door,HttpServletResponse response) throws IOException {
        int row = doorService.toUpdateDoor(door);
        if (row<=0) {
            response.setContentType("text/html;charset=utf-8");
            PrintWriter writer = response.getWriter();
            writer.println("<script>alert('修改失败');location.href='findAllDoorInfo';</script>");
            writer.close();
        }
        //重定向
        return "redirect:findAllDoorInfo";
    }




}
